package ioc

import (
	"github.com/gin-gonic/gin"
	"go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
	"guafo/internel/web"
	"guafo/internel/web/middlewares"
	"guafo/internel/web/middlewares/jwtx"
	"guafo/pkg/ginl/middleware/metric"
)

// @Description
// @Author 代码小学生王木木

func InitGin(r *web.Router, mdls []gin.HandlerFunc) *gin.Engine {
	server := gin.Default()
	server.Use(mdls...)
	r.RegisterRouter(server)
	return server
}

func InitMiddleware(cfg *Config, j jwtx.JwtHandler) []gin.HandlerFunc {
	return []gin.HandlerFunc{
		middlewares.CorsMiddleWare(),
		middlewares.NewLoginJWTMiddlewareBuilder(j).
			IgnorePaths("/sign-up").
			IgnorePaths("/pwd-login").
			IgnorePaths("/code").
			Build(),
		(&metric.MiddlewareBuilder{
			Namespace:  "test",
			Subsystem:  "wll_nb",
			Name:       cfg.ServiceName,
			Help:       "统计 GIN 的 HTTP 接口",
			InstanceID: cfg.ServiceName + "_test_1",
		}).Build(),
		otelgin.Middleware(cfg.ServiceName), // 注入otel 链路追踪
	}
}
